package io.renren.modules.app.controller.user.user;

import com.aliyun.oss.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.YcUserEntity;
import io.renren.modules.app.entity.YcUserResumeEntity;
import io.renren.modules.app.service.YcUserResumeService;
import io.renren.modules.app.service.YcUserService;
import io.renren.modules.app.utils.ALiSMSUtil;
import io.renren.modules.app.utils.FileUploadUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;




@RestController
@RequestMapping("user")
public class YcUserController {
    @Autowired
    private YcUserService ycUserService;
    @Autowired
    YcUserResumeService ycUserResumeService;
    //文件上传工具类
    @Resource
    private FileUploadUtil fileUploadUtil;
    String hd;


    /**
     * 列表
     */
    @PostMapping("/loginUser")
    public R loginUser(@RequestParam Map<String, Object> map){
        QueryWrapper<YcUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_name", map.get("loginAccountName"))
                .eq("password", map.get("loginPassword"));

        YcUserEntity ycUserEntity = ycUserService.getOne(queryWrapper);
        System.out.println("====="+ycUserEntity);


        if (ycUserEntity ==null)
            return R.error(-1,"没有找到用户名密码");
        else {
            return R.ok().put("userId" , ycUserEntity.getId());

        }

    }
    @PostMapping("/qulick")
    public R qulick(@RequestParam Map<String, Object> map){
        String code = (String) map.get("code");
        System.out.println(hd);
        if (code.equals(hd) ){
            return R.error(-1,"登录成功");
        }else {
            return R.error(0,"输入有误");
        }


    }
    @PostMapping("/smsCode")
    public String smsCode(@RequestParam String mobile  ) throws ClientException, com.aliyuncs.exceptions.ClientException {

        String sendCode = ALiSMSUtil.sendCode(mobile);
        hd = sendCode;


        return sendCode;
    }




    @PostMapping("/registerUser")
    public R registerUser(@RequestParam Map<String, Object> map) {
        String accountName = (String) map.get("accountName");
        String password = ((String) map.get("password"));
        String mobile = ((String) map.get("mobile"));

        //非空验证
//        QueryWrapper<YcUserEntity> query = new QueryWrapper<>();
//        query.eq("accountName",map.get("accountName"))
//                .eq("password",map.get("password"))
//                .eq("mobile",map.get("mobile"));
//        YcUserEntity ep = ycUserService.getOne(query);
//        if (ep==null){
//            return R.error("有空请重新输入");
//        }else {
//            return R.ok("输入正确");
//        }




        //判断手机号是否存在
        QueryWrapper<YcUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("mobile", map.get("mobile"));

        YcUserEntity user = ycUserService.getOne(queryWrapper);
        if (user == null) {

            //注册逻辑:

            YcUserEntity ycUserEntity = new YcUserEntity();
            ycUserEntity.setAccountName(accountName);
            ycUserEntity.setPassword(password);
            ycUserEntity.setMobile(mobile);

            boolean result = ycUserService.save(ycUserEntity);
            if (result) {
                return R.ok("成功注册");
            } else {
                return R.error("注册失败");
            }


        } else {
            return R.error(0, "手机号已经被注册");
        }
    }
    @ApiOperation("查询个人简历是否为空")
    public int selectUserResume(Integer userId) {
        QueryWrapper<YcUserResumeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        YcUserResumeEntity userResume = ycUserResumeService.getOne(queryWrapper);
        return (userResume == null) ? 0 : 1;
    }

    @ApiOperation("获取用户个人信息")
    @RequestMapping("/Individual")
    public R getIndividual(@RequestParam("userId") Integer userId) {
        YcUserEntity user = ycUserService.getIndividual(userId);
        return R.ok().put("Individual", user);
    }
    @ApiOperation("保存用户个人信息")
    @RequestMapping("/saveIndividual")
    public R saveIndividual( YcUserEntity user, YcUserResumeEntity userResume,@RequestParam("user_individual") String userIndividual, @RequestParam("file") MultipartFile file) {
        System.out.println(file);
        // 保存文件
        String filePath = null;
        try {
            filePath = fileUploadUtil.saveFile(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(filePath);
        //设置用户头像
        user.setUserPortrait(filePath);
        user.setUserIndividual(userIndividual);
        ycUserService.updateById(user);
        // 设置简历头像
        userResume.setResumePortrait(filePath);
        ycUserResumeService.update(userResume, new UpdateWrapper<YcUserResumeEntity>().eq("user_id", user.getId()));
        return R.ok();
    }
}
